package com.chen.blue.controller.admin.system;

import com.chen.blue.domain.model.system.SysDictData;
import com.chen.blue.result.DataResult;
import com.chen.blue.result.R;
import com.chen.blue.result.TableDataInfo;
import com.chen.blue.service.system.ISysDictDataService;

import com.chen.blue.service.system.ISysDictTypeService;
import com.chen.blue.service.security.SecurityService;
import com.chen.blue.utils.ExcelUtil;
import com.chen.blue.utils.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.chen.blue.utils.TableDateUtil.getDataTable;


/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@Slf4j
@RequestMapping("/system/dict/data")
@RestController
@Api(tags = "字典数据模块")
public class SysDictDataController {

    @Resource
    private ISysDictDataService dictDataService;

    @Resource
    private ISysDictTypeService dictTypeService;

    @ApiOperation("获取字典数据列表")
    @GetMapping("/list")
    public R<DataResult<List<SysDictData>>> list(SysDictData dictData) {

        return R.success(dictDataService.selectDictDataList(dictData));

    }

    @ApiOperation("导出数据字典数据")
    @PostMapping("/export")
    public void export(SysDictData dictData, HttpServletResponse response) throws IOException {

        List<SysDictData> list = (List<SysDictData>) dictDataService.selectDictDataList(dictData).getResult();
        ExcelUtil.export(response, SysDictData.class, String.valueOf(System.currentTimeMillis()), "字典数据", list);

    }

    /**
     * 查询字典数据详细
     */
    @ApiOperation("查询字典数据详细")
    @GetMapping(value = "/{dictCode}")
    public R<SysDictData> getInfo(@PathVariable Long dictCode) {

        return R.success(dictDataService.selectDictDataById(dictCode));

    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation("根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public R<List<SysDictData>> dictType(@PathVariable String dictType) {

        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (Objects.isNull(data)) {
            data = new ArrayList<SysDictData>();
        }
        return R.success(data);

    }

    /**
     * 新增字典类型
     */
    @ApiOperation("新增字典数据")
    @PostMapping
    public R add(@Validated @RequestBody SysDictData dict) {

        dict.setIsDefault(0);
        dict.setCreateBy(SecurityService.currentUserName());
        return (dictDataService.insertDictData(dict)) > 0 ? R.success() : R.faild();

    }

    /**
     * 修改保存字典类型
     */
    @ApiOperation("修改保存字典数据")
    @PutMapping
    public R edit(@Validated @RequestBody SysDictData dict) {

        dict.setUpdateBy(SecurityService.currentUserName());
        return (dictDataService.updateDictData(dict)) > 0 ? R.success() : R.faild();

    }

    /**
     * 删除字典类型
     */
    @ApiOperation("删除字典类型")
    @DeleteMapping("/{dictCodes}")
    public R remove(@PathVariable Long[] dictCodes) {

        dictDataService.deleteDictDataByIds(dictCodes);
        return R.success();

    }
}
